package com.example.message_queue.mqserver.core;

import com.example.message_queue.common.ConsumerEnv;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-03-23
 * Time:14:22
 * Text：队列
 */

public class MSGQueue {
    //name作为队列的身份标识
    private String name;
    //durable表示队列是否需要持久化存储，
    private boolean durable = false;
    //独占功能，表示可被一个消费者专门使用，也可以大家一起使用
    private boolean exclusive = false;
    //true表示没有人使用，自动删除
    private boolean autoDelete = false;
    private Map<String,Object> arguments=new HashMap<>();

    //表示当前有哪些消费者订阅了
    private List<ConsumerEnv> consumerEnvsList =new ArrayList<>();
    //记录当前取到了几个消费者，方便轮询策略
    private AtomicInteger consumerSeq = new AtomicInteger(0);
    //添加一个订阅者
    public void addConsumerEnv(ConsumerEnv consumerEnv){
        consumerEnvsList.add(consumerEnv);
    }
    //订阅者的删除不考虑
    //挑选一个订阅者，处理当前的消息
    public ConsumerEnv chooseConsumer(){
        if(consumerEnvsList.size() == 0)
            return null;
        int index = consumerSeq.get()%consumerEnvsList.size();
        consumerSeq.getAndIncrement();
        return consumerEnvsList.get(index);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDurable() {
        return durable;
    }

    public void setDurable(boolean durable) {
        this.durable = durable;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    public void setExclusive(boolean exclusive) {
        this.exclusive = exclusive;
    }

    public boolean isAutoDelete() {
        return autoDelete;
    }

    public void setAutoDelete(boolean autoDelete) {
        this.autoDelete = autoDelete;
    }

    public String getArguments() {
        //将当前的arguments参数，从map转化为String
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "{}";
    }
    //从数据库中读取时，构造Exchange对象
    public void setArguments(String argumentsJson){
        //把参数中的argumentsJson按照为json格式解析为map对象
        ObjectMapper objectMapper=new ObjectMapper();

        try {
            this.arguments=objectMapper.readValue(argumentsJson, new TypeReference<HashMap<String,Object>>() {});
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }


    public void setArguments(Map<String,Object> arguments){
        this.arguments = arguments;
    }


    //测试用例的键值对
    public Object getArguments(String Key) {
        return arguments.get(Key);
    }
    public void setArguments(String Key,Object value){
        arguments.put(Key,value);
    }
}
